package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.ManualAcctInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.ManualAcctInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.ManualAcctInfo;
import cc.rengu.igas.bomp.common.enums.AcctStatusEnum;
import cc.rengu.igas.bomp.common.enums.AuditStatusEnum;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.PayService;
import cc.rengu.igas.bomp.facade.request.ManualPaymentRequest;
import cc.rengu.igas.bomp.facade.response.ManualPaymentResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * 手工挂账服务
 * Created by 王成 on 2020/3/21
 */
public class ManualPaymentService extends PayService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        ManualPaymentRequest manualPaymentRequest = new ManualPaymentRequest();
        ConvertUtil.convertOutput(manualPaymentRequest);
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.ORDER_AMT, manualPaymentRequest.getAdjustAmt());
        return manualPaymentRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        ManualPaymentRequest manualPaymentRequest = (ManualPaymentRequest) request;
        ManualPaymentResponse manualPaymentResponse = new ManualPaymentResponse();
        BeanUtil.beanCopy(manualPaymentRequest, manualPaymentResponse);
        BizResponse bizResponse = new BizResponse();

        /* 根据挂账唯一标识校验挂账状态 */
        ManualAcctInfoMapper manualAcctInfoMapper = new ManualAcctInfoMapperImpl();
        ManualAcctInfo manualAcctInfo = manualAcctInfoMapper.selectManualAcctInfoById(manualPaymentRequest.getHeader().getInstId(), manualPaymentRequest.getAcctUnionKey());
        if (null == manualAcctInfo) {
            rglog.error("挂账记录不存在，不允许发起挂账请求，instId:<{}>,acctUnionKey:<{}>", manualPaymentRequest.getHeader().getInstId(), manualPaymentRequest.getAcctUnionKey());
            throw new BizException(RespCodeEnum.RECORD_NOT_FOUND.getRespCode(), RespCodeEnum.RECORD_NOT_FOUND.getRespDesc());
        }
        /* 校验当前记录状态，只有审核通过且未记账状态才允许发起挂账请求 */
        if ((AuditStatusEnum.ADD_AUDIT_SUCCESS.getAuditStatus().equals(manualAcctInfo.getAuditStatus()) || AuditStatusEnum.MODIFY_AUDIT_SUCCESS.getAuditStatus().equals(manualAcctInfo.getAuditStatus())) &&
                (!AcctStatusEnum.SUCCESS.getAcctStatus().equals(manualAcctInfo.getAcctStatus()))) {

            //TODO 调用挂账接口
            // （1） 超时,更新手工挂账记账状态（超时）updateManualAcctStatusAndAuditSeqNum，返回超时
            // （2） 成功,更新手工挂账记账状态（成功）updateManualAcctStatus，返回超时
            // （3） 失败，更新手工挂账记账状态（失败）updateManualAcctStatusAndAuditSeqNum，
        } else {
            rglog.error("当前记录状态不允许发起挂账请求,auditStatus:<{}>,acctStatus:<{}>", manualAcctInfo.getAuditStatus(), manualAcctInfo.getAcctStatus());
            throw new BizException(RespCodeEnum.ILLEGAL_OPERATION.getRespCode(), RespCodeEnum.ILLEGAL_OPERATION.getRespDesc());
        }
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        ManualPaymentResponse manualPaymentResponse = (ManualPaymentResponse) bizResponse.getResult();
        ConvertUtil.convertInput(manualPaymentResponse);
    }

    /**
     * 更新手工挂账记账状态（超时）
     *
     * @param manualAcctInfo 手工挂账记录
     * @throws Exception 异常
     */
    private void updateManualAcctStatusAndAuditSeqNum(ManualAcctInfo manualAcctInfo) throws Exception {
        ManualAcctInfoMapper manualAcctInfoMapper = new ManualAcctInfoMapperImpl();
        int iReturnCode = manualAcctInfoMapper.updateManualAcctInfoById(manualAcctInfo);
        if (0 != iReturnCode) {
            rglog.error("更新手工挂账状态失败,instId:<{}>,id:<{}>,adjustAmt:<{}>,acctDate:<{}>,acctSeqNum:<{}>,iReturnCode:<{}>",
                    manualAcctInfo.getInstId(), manualAcctInfo.getId(), manualAcctInfo.getAdjustAmt(), manualAcctInfo.getAcctDate(), manualAcctInfo.getAcctSeqNum(), iReturnCode);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }


    }
}
